﻿/***************************************************/
/*** file:Session.cs                             ***/
/*** date:2010-08-15                             ***/
/*** author:huas                                 ***/
/*** function:session of client                  ***/
/***************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using IM.Common;

namespace IM.Logic
{
    class Session
    {
        private int m_nThreadIndex; //处理该session所在的线程
        private string m_strAccount; //用户账号
        private int m_nAccountId;
        private NetHeader m_netHeader; //头部信息，（包含该用户所在socket信息）
        private IPAddress m_ipAddr;
        private IMAccountStatus m_accountStatus;

        private IMLogicHandle m_logicHandle;
        private ProtocolMgr m_protocolMgr;
        private SessionMgr m_sessionMgr;

        public Session()
        {
            m_nThreadIndex = -1;
            m_nAccountId = 0;
            m_strAccount = string.Empty;
            m_ipAddr = new IPAddress(0);
            m_accountStatus = IMAccountStatus.im_status_online;
            m_sessionMgr = null;
        }

        public SessionMgr SessionMgr
        {
            set
            {
                m_sessionMgr = value;
            }
        }

        public IMAccountStatus AccountStatus
        {
            get
            {
                return m_accountStatus;
            }
            set
            {
                m_accountStatus = value;
            }
        }

        public string Account
        {
            get
            {
                return m_strAccount;
            }
            set
            {
                m_strAccount = value;
            }
        }

        public int ThreadIndex
        {
            get
            {
                return m_nThreadIndex;
            }
            set
            {
                m_nThreadIndex = value;
            }
        }

        public int AccountId
        {
            get
            {
                return m_nAccountId;
            }
            set
            {
                m_nAccountId = value;
            }
        }

        public NetHeader NetHeader
        {
            get
            {
                return m_netHeader;
            }
            set
            {
                m_netHeader = value;
            }
        }

        public void Initialize(ProtocolMgr protocolMgr, IMLogicHandle logicHandle)
        {
            m_protocolMgr = protocolMgr;
            m_logicHandle = logicHandle;
        }

        private int SendPack(byte[] bData, ref NetHeader netHeader)
        {
            return m_logicHandle.SendToClient(bData, bData.Length, ref netHeader);
        }

        //其他协议处理
        public int ProcessRequest(IMProtocolDef protocolId, byte[] bdata)
        {
            int nRet = -1;
            switch (protocolId)
            {
                    //发送聊天消息
                case IMProtocolDef.im_c2s_account_chat:
                    nRet = OnAccountChat(bdata);
                    break;
                case IMProtocolDef.im_c2s_account_logout:
                    nRet = OnUserLogout(bdata);
                    break;
            }
            return 0;
        }

        private int OnUserLogout(byte[] bData)
        {
            int nRet = -1;
            c2s_account_logout accountLogout = m_protocolMgr.c2s_account_logout;
            accountLogout.FromBytes(bData);

            Session session = null;
            nRet = m_sessionMgr.FindSession(accountLogout.szAccount, ref session);

            //找到了，直接转发
            if (nRet == 0)
            {
                accountLogout.ProtocolId = (ushort)IMProtocolDef.im_c2s_account_logout;
                byte[] bSendData = accountLogout.ToBytes();
                NetHeader netHeader = session.NetHeader;
                nRet = SendPack(bSendData, ref netHeader);
            }
            return nRet;
        }
        /// <summary>
        ///发送用户聊天信息,需要转发到接收消息的
        ///账号去
        /// </summary>
        /// <returns>0 suc -1 failed</returns>
        private int OnAccountChat(byte[] bData)
        {
            int nRet = -1;
            c2s_account_chat accountchat = m_protocolMgr.c2s_account_chat;
            accountchat.FromBytes(bData);

            Session session = null;
            nRet = m_sessionMgr.FindSession(accountchat.szAccount, ref session);
            
            //找到了，直接转发
            if (nRet == 0)
            {
                accountchat.ProtocolId = (ushort)IMProtocolDef.im_s2c_account_chat;
                byte[] bSendData = accountchat.ToBytes();
                NetHeader netHeader = session.NetHeader;
                nRet = SendPack(bSendData, ref netHeader);
            }
            return nRet;
        }
    }
}
